﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.BloodGlucoseMeter
{
    public partial class AddBloodGlucoseMeter : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;   //员工信息
        string Id = string.Empty;
        PatientInfo Patientinfo;    //患者信息
        #endregion

        #region 构造函数
        public AddBloodGlucoseMeter(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public AddBloodGlucoseMeter(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public AddBloodGlucoseMeter(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            //绑定数据
            BindInfo(this.Id);
        }
        #endregion

        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(txtDate.Text.Trim()))
                txtDate.DateTime = DateTime.Now;

            //时间判断
            if (!txtTime.Text.Contains(":"))
            {
                txtTime.Focus();
                MessageBox.Show("时间需HH:mm格式，或使用双击显示当前时间");
                return;
            }
            txtTime.Text = DateTimeTran.TimeFillZero(txtTime.Text);
            if (string.IsNullOrEmpty(txtTime.Text))
                txtTime.Text = DateTime.Now.ToString("HH:mm");

            BloodMeter dhs = new BloodMeter();
            dhs.JianCeRiQi = txtDate.DateTime.ToString("yyyy-MM-dd");   //日期
            dhs.ShiJian = DateTimeTran.TimeFillZero(txtTime.Text);    //时间
            dhs.XueTangZhi = txtXuetangzhi.Text;     //血糖值
            if (string.IsNullOrEmpty(Id))   //签名
                dhs.QianMing = LoginUser.UserName;
            else
                dhs.QianMing = txtQianming.Text;

            dhs.FormId = (int)FormType.BloodGlucoseMeter;   //表单Id
            dhs.FormName = EnumHelper.GetDescription(FormType.BloodGlucoseMeter);   //表单名字
            record.FormId = (int)FormType.BloodGlucoseMeter;   //表单Id
            record.FormName = EnumHelper.GetDescription(FormType.BloodGlucoseMeter);   //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;  //住院号
            record.VisitId = Patientinfo.VISIT_ID;   //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;   //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;   //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;   //当前患者病区名称
            record.AssessmentDate = Convert.ToDateTime(dhs.JianCeRiQi + " " + dhs.ShiJian);    //日期+时间
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(dhs, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 显示赋值
        private void BindInfo(string Id)
        {
            txtDate.DateTime = DateTime.Now;     //默认显示当前的值
            txtQianming.Text = LoginUser.UserName;     //签名获取当前登录工号者
            txtTime.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            BloodMeter bm = new BloodMeter();
            bm = XmlUtil.XMLToObject<BloodMeter>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(bm.JianCeRiQi))
                txtDate.DateTime = Convert.ToDateTime(bm.JianCeRiQi);
            txtTime.Text = DateTimeHelper.HHmmssToHHmm(bm.ShiJian);   //时间
            txtXuetangzhi.Text = bm.XueTangZhi;    //血糖值
            txtQianming.Text = bm.QianMing;   //签名
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 双击获取当前时间
        private void txtTime_DoubleClick(object sender, EventArgs e)
        {
            txtTime.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion
    }
}
